package it.abc.sicsic.model.spese;

import it.abc.sicsic.model.Constants;

import it.abc.sicsic.model.util.Util;

import java.io.Serializable;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import javax.xml.crypto.Data;


@Entity
@NamedQueries( { @NamedQuery(name = "Spese.findAll", query = "select o from Spese o") })
@Table(name = "\"spese\"")
public class Spese implements Serializable {
    
    public final static String FIND_SPESE = 
        "SELECT * FROM SPESE " +
        "WHERE (DATA LIKE ? OR ? IS NULL) " +
        "AND (BARCA LIKE ? OR ? IS NULL)";
    
    public final static String FIND_SPESE_PER_MESE = 
    "SELECT date(data) AS DATA,MONTH(data) AS MESE," +
    "id,importoSpese,barca,note,pagamento,(SELECT SUM(importoSpese) " +
    "FROM nauticasicsic.spese WHERE MONTH(data) = MESE)  AS SubTotale " +
    "FROM nauticasicsic.spese";
    
    @Temporal(TemporalType.DATE)
    @Column(name="DATA")
    private Date dataFormatDate;
    @Temporal(TemporalType.DATE)
    @Column(name="MONTH")
    private Date mese;
    @Column(name="SubTotale")
    private int subTotale;
    
    @Column(name="barca")
    private String barca;
    @Column(name="data")
    private String data;
    @Column(name="descrizione")
    private String descrizione;
    @Id
    @Column(name="id", nullable = false)
    private int id;
    @Column(name="importoSpese")
    private int importoSpese;
    @Column(name="note")
    private String note;
    @Column(name="pagamento")
    private boolean pagamento;
    @Transient
    private String style="";    
    @Transient
    private String flagSubtotal="n";
    @Transient
    private Date formattedDate;
    
    public boolean isSubtotal(){
        return getFlagSubtotal().equals("y");
    }
    
    public Spese() {
    }

    public Spese(String barca, String data, String descrizione, int id, int importoSpese, String note,
                 boolean pagamento) {
        this.barca = barca;
        this.data = data;
        this.descrizione = descrizione;
        this.id = id;
        this.importoSpese = importoSpese;
        this.note = note;
        this.pagamento = pagamento;
    }


    public String getBarca() {
        return barca;
    }

    public void setBarca(String barca) {
        this.barca = barca;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getImportoSpese() {
        return importoSpese;
    }

    public void setImportoSpese(int importoSpese) {
        this.importoSpese = importoSpese;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public boolean isPagamento() {
        return pagamento;
    }

    public void setPagamento(boolean pagamento) {
        this.pagamento = pagamento;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(getClass().getName()+"@"+Integer.toHexString(hashCode()));
        buffer.append('[');
        buffer.append("barca=");
        buffer.append(getBarca());
        buffer.append(',');
        buffer.append("data=");
        buffer.append(getData());
        buffer.append(',');
        buffer.append("descrizione=");
        buffer.append(getDescrizione());
        buffer.append(',');
        buffer.append("id=");
        buffer.append(getId());
        buffer.append(',');
        buffer.append("importoSpese=");
        buffer.append(getImportoSpese());
        buffer.append(',');
        buffer.append("note=");
        buffer.append(getNote());
        buffer.append(',');
        buffer.append("pagamento=");
        buffer.append(isPagamento());
        buffer.append("subTotale=");
        buffer.append(getSubTotale());
        buffer.append(']');
        return buffer.toString();
    }

    public void setFlagSubtotal(String flagSubtotal) {
        this.flagSubtotal = flagSubtotal;
    }

    public String getFlagSubtotal() {
        return flagSubtotal;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getStyle() {
        return isSubtotal() ? Constants.Style.SUBTOTAL : ""; 
    }
    
    public Date getFormattedDate(){
        return Util.convertStringToDate(getData(), Constants.DatePatterns.yyyyMMdd);
    }

    public void setDataFormatDate(Date dataFormatDate) {
        this.dataFormatDate = dataFormatDate;
    }

    public Date getDataFormatDate() {
        return dataFormatDate;
    }

    public void setMese(Date mese) {
        this.mese = mese;
    }

    public Date getMese() {
        return mese;
    }

    public void setSubTotale(int subTotale) {
        this.subTotale = subTotale;
    }

    public int getSubTotale() {
        return subTotale;
    }
}
